
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.odps.udf.UDF;
import com.aliyun.odps.utils.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class SpliderCultBeautyGoods extends UDF{
	/*public static void main(String[] args) throws Exception {
		HttpClientAPI HttpClientAPI = new HttpClientAPI();
		HttpResult doGet = HttpClientAPI.doGet("https://www.cultbeauty.co.uk/hourglass-veil-mineral-primer.html");
		Document document = Jsoup.parse(doGet.getBody());
		String html = document.getElementsByClass("bigSiteContainer").toString();
		evaluate(html);
	}*/

	public String evaluate(String html) {
		try {

			JSONObject returnObj = new JSONObject();
			if (StringUtils.isNotBlank(html)) {
				Document doc = Jsoup.parse(html);
				//获取商品品牌和名称
				Elements brandClass = doc.getElementsByClass("productBrandTitle");
				if(null != brandClass && brandClass.size() > 0){
					String brand = brandClass.text();
					System.out.println(brand);
					returnObj.put("brand", brand);
				}
				Elements titleClass = doc.getElementsByClass("productTitle");
				if(null != titleClass && titleClass.size() > 0){
					String title = titleClass.text();
					System.out.println(title);
					returnObj.put("title", title);
				}

				//获取目录
				Elements cateClass = doc.getElementsByClass("breadcrumb");
				if(null != cateClass && cateClass.size() == 1) {
					//System.out.println(cateClass.get(0).text());
					Elements liTags = cateClass.get(0).getElementsByTag("li");
					if(null != liTags && liTags.size() != 0) {
						String text = liTags.get(liTags.size() - 1).text();
						System.out.println(text);
						StringBuffer sb = new StringBuffer();
						for(Element e :liTags) {
							sb.append(e.text()).append(">");
						}
						String category = sb.substring(0, sb.lastIndexOf(">"));
						System.out.println("目录 ："+category);
						returnObj.put("category", category);
					}
					
				}
				
				//获取价格  （有多个价格，划线价格不要）
				Elements priceClass = doc.getElementsByClass("price-box");
				if(null != priceClass && priceClass.size() == 1) {
					Elements priceEles = priceClass.get(0).getElementsByClass("productSpecialPrice js-product-special-price");
					if(null != priceEles && priceEles.size() != 0){
						//获取价格符号
						String text = priceClass.get(0).getElementsByClass("productCurrency productSpecialCurrency").text();
						//获取价格
						String price = priceEles.text();
						returnObj.put("price",text+price);
					}else{
						String price = priceClass.get(0).text();
						System.out.println(priceClass.get(0).text());
						returnObj.put("price", price);
					}

				}
				
				//获取尺寸
				Elements sizeClass = doc.getElementsByClass("productSize");
				if(null != sizeClass && sizeClass.size() != 0) {
					String size = sizeClass.get(0).text();
					if(StringUtils.isNotBlank(size)) {
						size = size.replace("\"","");
						size = size.replace("(","");
						size = size.replace(")","");
						size = size.trim();
						System.out.println(size);
						returnObj.put("size", size);
					}
				}

				//部分产品有多个价格容量字段  用一个新字段存取
				Elements productActionOptions = doc.getElementsByClass("productActionOptions");
				if(null != productActionOptions && productActionOptions.size() == 1) {
					Elements liEles = productActionOptions.get(0).getElementsByTag("li");
					if(null != liEles && liEles.size() > 0){
						StringBuffer sbText = new StringBuffer();
						for (Element li :liEles){
							sbText = sbText.append(li.text()).append("@@");
						}
						returnObj.put("otherContent", sbText.substring(0,sbText.lastIndexOf("@@")));
					}
				}
				
				//在获取图片之前要获取商品id
				Elements idElements = doc.getElementsByClass("productAlterationsContainer js-product-alterations");
				String productId = null;
				if(null != idElements && idElements.size() == 1) {
					productId = idElements.get(0).attr("data-product-id");
					returnObj.put("goodsId", productId);
				}
				
				//获取图片   大图和小图
				Elements minImgClass = doc.getElementsByClass("thumb js-select-thumb product-id-"+productId);
				//System.out.println(minImgClass.size());
				if(null != minImgClass && minImgClass.size() != 0) {
					StringBuffer bigImgSb = new StringBuffer();
					StringBuffer smallImgSb = new StringBuffer();
					for(Element e :minImgClass) {
						String bigImg = e.attr("data-big-image");
						bigImgSb.append(bigImg).append(",");
						//System.out.println("大图地址："+bigImg);
						Elements elementsByTag = e.getElementsByTag("img");
						String smallIng = elementsByTag.get(0).attr("src");
						smallImgSb.append(smallIng).append(",");
						//System.out.println("小图地址："+smallIng);
					}
					String bigImgHerf = bigImgSb.substring(0, bigImgSb.lastIndexOf(","));
					String smallImgHerf = smallImgSb.substring(0, smallImgSb.lastIndexOf(","));
					System.out.println("大图地址："+bigImgHerf);
					System.out.println("小图地址："+smallImgHerf);
					returnObj.put("bigImg", bigImgHerf);
					returnObj.put("smallImg", smallImgHerf);
				}else{
					//上面没获取到图片就说明网站的图片不标准 通过其他的标签获取图片
					Elements bigImgClass = doc.getElementsByClass("bigImage js-big-image-wrapper");
					Elements imgTag = bigImgClass.get(0).getElementsByTag("img");
					String bigImgSrc = imgTag.get(0).attr("src");
					returnObj.put("bigImg", bigImgSrc);
				}
				
				//获取部分描述内容  
				Elements descp = doc.getElementsByClass("productInfoInner");
				if(null != descp && descp.size() == 1) {
					Elements liTags = descp.get(0).getElementsByTag("li");
					if(null != liTags && liTags.size() != 0) {
						for (int i = 0; i < 4; i++)  {
							Element element = liTags.get(i);
							//获取标题
							String head = element.getElementsByClass("itemHeader").text();
                            System.out.println(head);
                            //处理成分
                            if(StringUtils.equals(head,"Full ingredients list")){
                                List<Map<String,String >> inredientList = new ArrayList<>();
                                Element itemContent = element.getElementsByClass("itemContent").get(0);
                                Elements pEles = itemContent.getElementsByTag("p");
                                for (Element e :pEles){
                                    Map<String,String > ingreMap = new HashMap<>();
                                    String text = e.text();
                                    if(StringUtils.isNotBlank(text) && text.contains(":")){
                                        ingreMap.put("goods_name",text.substring(0,text.indexOf(":")));
                                        ingreMap.put("goods_ingredient",text.substring(text.indexOf(":")+1));
                                    }else {
                                        ingreMap.put("goods_name","");
                                        ingreMap.put("goods_ingredient",text);
                                    }
                                    inredientList.add(ingreMap);
                                }
                                System.out.println(JSON.toJSONString(inredientList));
                                returnObj.put(head, inredientList);
                            }else{
								//获取内容 内容比较多根据标签p分段
								Element itemContent = element.getElementsByClass("itemContent").get(0);
								Elements pEles = itemContent.getElementsByTag("p");
								StringBuffer sbContent = new StringBuffer();
								for (Element e :pEles){
									sbContent.append(e.text()).append("@@");
								}
								String content = sbContent.substring(0,sbContent.lastIndexOf("@@"));
								System.out.println(content);
								returnObj.put(head, content);
							}

						}
					}
				}
				
			}
			System.out.println(returnObj.toJSONString());
			return returnObj.toJSONString();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return null;
	}
}
